1619D - New Year's Problem - CodeForces Solution


binary search greedy sortings *1800

Please click on ads to support us..

Python Code:

t=int(input())
while t:
    def fun(num,n,m,li):
        arr=[False]*m
        flag=0
        for i in range(n):
            c=0
            for j in range(m):
                if li[i][j]>=num:
                    arr[j]= True
                    c+=1
            if c>1:
                flag= 1
        if not flag and m>1:
            return False
        ans= True
        for i in arr:
            ans= ans and i
        return ans
    space= input()
    n,m= map(int,input().split())
    li=[]
    for i in range(n):
        a= list(map(int,input().split()))
        li.append(a)
    low= 1
    high=2
    while fun(high,n,m,li):
        high*=2
    while high-low>1:
        mid= (low+high)//2
        if fun(mid,n,m,li):
            low= mid
        else:
            high= mid
    print(low)
    t-=1

C++ Code:

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

void solve(){
    int m,n;
    cin >> m >> n;
    vector<vector<ll> > p(m,vector<ll> (n));
    for(ll i =0;i<m;i++)
    {
        for(ll j=0;j<n;j++)
        {
            ll x;
            cin >> x;
            p[i][j] = x;
        }
    }
    ll alpha = 1e9;

     for(ll i =0;i<n;i++)
    {
        ll col_max = 0;
        for(ll j=0;j<m;j++)
        {
        col_max = max(col_max,p[j][i]);
        }
        alpha = min(alpha,col_max);
    }

    ll alp = 0;
    for(ll i =0;i<m;i++)
    {
        ll first = 0;
        ll second = 0;
        for(ll j=0;j<n;j++)
        {
            if(p[i][j]>first)
            {
                second = first;
                first= p[i][j];
            }
            else if(p[i][j]>second){
                second = p[i][j];
            }
        }
        alp = max(alp,second);
    }
    alpha = min(alp,alpha);
    cout << alpha << endl;


}
int main(void){
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph